import zlib from "node:zlib";
import fs from "node:fs";
import stream from "node:stream/promises";

class Compress {
  // gzip压缩文件，返回promise函数
  static async do_gzip(input, output) {
    const gzip = zlib.createGzip();
    const readStream = fs.createReadStream(input);
    const writeStream = fs.createWriteStream(output);
    await stream.pipeline(readStream, gzip, writeStream);
  }

  // deflate 压缩文件
  static async do_deflate(input, output) {
    const deflate = zlib.createDeflate();
    const readStream = fs.createReadStream(input);
    const writeStream = fs.createWriteStream(output);
    await stream.pipeline(readStream, deflate, writeStream);
  }
}

class Decompress {
  // ungzip 解压文件
  static async do_ungzip(input, output) {
    const ungzip = zlib.createUnzip();
    const readStream = fs.createReadStream(input);
    const writeStream = fs.createWriteStream(output);
    await stream.pipeline(readStream, ungzip, writeStream);
  }

  // inflate 解压文件
  static async do_inflate(input, output) {
    const inflate = zlib.createInflate();
    const readStream = fs.createReadStream(input);
    const writeStream = fs.createWriteStream(output);
    await stream.pipeline(readStream, inflate, writeStream);
  }
}

export { Compress, Decompress };
